package au.com.zonski.gwlite.j2me.client;

import net.jscience.math.kvm.MathFP;


public class StunController implements Controller 
{
	private int animationDivisor;
	private int stunDuration;
	private boolean moveOnFinish;
	private SeekerController seekerController;
	private int[] scoreColors = {
			0xFFFFDD, 
			0xEEEECC, 
			0xDDDDBB, 
			0xEEEECC, 
			0xDDDDBB,
			0xBBBBAA,
			0xCCCCAA, 
			0xBBBBAA,
			0xAAAA99, 
			0x999977,
			0x888866,
			0x777755,
			0x666644,
			0x555533,
			0x444422,
			0x555533,
			0x555533,
			0x333311,
			0x222200,
			0x111100
	};
	
	public StunController( int stunDuration, boolean moveOnFinish, int animationDivisor, SeekerController seekerController )
	{
		this.stunDuration = stunDuration;
		this.moveOnFinish = moveOnFinish;
		this.animationDivisor = animationDivisor;
		this.seekerController = seekerController;
	}
	
	public boolean collision(GWLiteCanvas gameState, GWEntity controlled, GWEntity with) 
	{
		// delegate to the actual controller for this object type 
		if( this.moveOnFinish )
		{
			gameState.controllers[ controlled.type ].collision( gameState, controlled, with );
		}
		return true;
	}

	public void learn(GWLiteCanvas gameState) 
	{
		// do nothing
	}

	public void reset() 
	{
		
	}
	
	public int[] initialise( GWLiteCanvas gameState, GWEntity o, int score, int dx, int dy )
	{
		// we're going to pause
		int[] controllerData;
		int rotation;
		if( o.type == GWEntity.TYPE_INTERCEPTOR )
		{
			this.seekerController.learn( gameState );
			int px = seekerController.targetX;
			int py = seekerController.targetY;
			int idx = px - (o.x + o.width/2);
			int idy = py - (o.y + o.height/2);
			try
			{
				rotation = GWLiteCanvas.atan2( idy, idx );
			}
			catch( Exception ex )
			{
				rotation = gameState.random.nextInt() % MathFP.PI;
			}
		}
		else if( o.type == GWEntity.TYPE_BLACKHOLE )
		{
			// TODO : this ain't right
			rotation = 3;
		}
		else
		{
			rotation = gameState.random.nextInt() % MathFP.PI;
		}
		controllerData = new int[]{ rotation, this.stunDuration, Math.abs( gameState.random.nextInt() % 4), score, this.scoreColors[0], dx, dy };
		o.controllerData = controllerData;
		return controllerData;
	}

	public boolean update(GWLiteCanvas gameState, GWEntity o) 
	{
		int[] controllerData;
		if( o.controllerData == null )
		{
			controllerData = initialise( gameState, o, 0, 0, 0 );
		}
		else
		{
			controllerData = (int[]) o.controllerData;
		}
		boolean remove;
		int timeRemaining = controllerData[1];
		int frame = (this.stunDuration - timeRemaining)/this.animationDivisor;
		int color = this.scoreColors[ Math.min(frame, this.scoreColors.length-1) ];  
		controllerData[4] = color;
		o.x += controllerData[5];
		o.y += controllerData[6];
		int animationStyle = gameState.animationStyle[ o.type ];
		if( controllerData[1] <= 0 && 
			( !this.moveOnFinish || animationStyle != 0 || gameState.sprites[ o.type ][0].getFrame() == 0 ) 
		)
		{
			// add them into their proper layer
			remove = true;
			if( this.moveOnFinish )
			{
				// it's rotated, we want to use this value
				if( animationStyle != 1 )
				{
					o.controllerData = null;
				}
				gameState.layers[ o.type ].addElement( o );
			}
		}
		else
		{
			if( !this.moveOnFinish )
			{
				controllerData[0] = frame;
			}
			controllerData[1] --;
			remove = false;
		}
		return remove;
	}

	
}
